package com.example.resourceSharingPlatform.security_dao;

import com.example.resourceSharingPlatform.security_entity.Users;
import com.example.resourceSharingPlatform.security_mappers.UsersRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 与数据库连接
 */
@Service
public class UsersDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /*
     * 根据userName查询users信息
     * parameter : String userName
     * */
    public List<Users> queryUsersByUserName(String userName){
        String sql = "SELECT * FROM users WHERE user_name=?";
        List<Users> itemList = jdbcTemplate.query(sql, new UsersRowMapper(), userName);
        return itemList;
    }
    /**
     * 插入users信息
     * parameter : String userName
     * */
    public Users insertUsers(Users users){
        String sql = "INSERT INTO users VALUES(?,?,?,?,?)";
        jdbcTemplate.update(sql, null, users.getUserName(), users.getPassword(), users.getStatus(), users.getRoles());
        String sql2 = "SELECT * FROM users WHERE user_name=?";
        return jdbcTemplate.queryForObject(sql2,new UsersRowMapper(), users.getUserName());
    }
    /**
     * 更新users信息
     * parameter : String userName
     * */
    public boolean updateUsers(Users users){
        String sql = "UPDATE users SET user_name=?, password=?, status=?, roles=?";
        int row = jdbcTemplate.update(sql,users.getUserName(),users.getPassword(),users.getStatus(),users.getRoles());
        return row != 0;
    }
    /**
     * 根据id删除users信息
     * parameter : String userName
     * */
    public boolean deleteUsersById(Users users){
        String sql = "DELETE FROM users WHERE user_id=?";
        int row = jdbcTemplate.update(sql, users.getUserId());
        return row != 0;
    }

}
